﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.AdvancedCustomerQuery
{
    public partial class ColumnControl : UserControl
    {
        private CustomerQuery.CustomerQueryClient client = null;
        public ColumnControl()
        {
            InitializeComponent();
            client = new CustomerQuery.CustomerQueryClient();
            client.DeleteQuery_ColumnCollectionCompleted += new EventHandler<CustomerQuery.DeleteQuery_ColumnCollectionCompletedEventArgs>(client_DeleteQuery_ColumnCollectionCompleted);
            client.GetQuery_ColumnCollectionCompleted += new EventHandler<CustomerQuery.GetQuery_ColumnCollectionCompletedEventArgs>(client_GetQuery_ColumnCollectionCompleted);
            LoadData();
        }

        void client_DeleteQuery_ColumnCollectionCompleted(object sender, CustomerQuery.DeleteQuery_ColumnCollectionCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("删除成功!");
                LoadData();
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("删除失败");
            }
        }

        void client_GetQuery_ColumnCollectionCompleted(object sender, CustomerQuery.GetQuery_ColumnCollectionCompletedEventArgs e)
        {
            dg.ItemsSource = null;
            dg.ItemsSource = e.Result;
        }

        private void NewBtn_Click(object sender, RoutedEventArgs e)
        {
            ColumnWindow win = new ColumnWindow(0);
            win.Closed -= new EventHandler(win_Closed);
            win.Closed += new EventHandler(win_Closed);
            win.Show();
        }

        void win_Closed(object sender, EventArgs e)
        {
            ChildWindow cw = sender as ChildWindow;
            if (cw != null && cw.DialogResult == true)
            {
                LoadData();
            }
        }

        private void EdtBtn_Click(object sender, RoutedEventArgs e)
        {
            var entity = dg.SelectedItem as CustomerQuery.Query_ColumnCollection;
            if (entity != null)
            {
                ColumnWindow win = new ColumnWindow(entity.ID);
                win.Closed -= new EventHandler(win_Closed);
                win.Closed += new EventHandler(win_Closed);
                win.Show();
            }
        }

        private void delBtn_Click(object sender, RoutedEventArgs e)
        {
            var entity = dg.SelectedItem as CustomerQuery.Query_ColumnCollection;
            if (entity != null)
            {
                client.DeleteQuery_ColumnCollectionAsync(entity.ID);
                
            }
        }

        private void LoadData()
        {
            client.GetQuery_ColumnCollectionAsync("1=1");
        }

      
    }
}
